const App = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    isLogin: false,
    teamId:0,    // 服务队ID（判断是否入会)
    userInfo: {}, // 用户信息
    orderCount: {}, // 订单数量
    team:{},
    unionAge:App.getConfig().userCenter.age
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
     //设置页面标题
     wx.setNavigationBarTitle({
      title: App.getConfig().userCenter.list
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let _this = this;
    _this.setData({
      isLogin: App.checkIsLogin(),
    });
    if(_this.data.isLogin){
      // _this.setData({
      //   teamId: App.checkTeam(),
      // });
       // 获取当前用户信息
      _this.getUserDetail();
    }
   
  },

  /**
   * 获取当前用户信息
   */
  getUserDetail() {
    let _this = this;
    App._get('user.index/detail', {}, function(result) {
      wx.setStorageSync('user_info',result.data.userInfo)
      _this.setData(result.data);
    });
  },

  /**
   * 订单导航跳转
   */
  onTargetOrder(e) {
    let _this = this;
    if (!_this.onCheckLogin()) {
      return false;
    }
    // 记录formid
    App.saveFormId(e.detail.formId);
    let urls = {
      all: '/pages/order/index?type=all',
      payment: '/pages/order/index?type=payment',
      received: '/pages/order/index?type=received',
      refund: '/pages/order/refund/index',
    };
    // 转跳指定的页面
    wx.navigateTo({
      url: urls[e.currentTarget.dataset.type]
    })
  },

  /**
   * 菜单列表导航跳转
   */
  onTargetMenus(e) {
    let _this = this;
    if (!_this.onCheckLogin()) {
      return false;
    }
    let linkArray =[
      "pages/user/news/index",
      "pages/user/activity/index",
      "pages/music/index",
      "pages/team/photo/index",
      "pages/shanghui/edit/index",
      "pages/shanghui/examine/index",
      "pages/leader/manager/index",
      "pages/introduce/manager/index",

    ]
    //验证权限
    if(linkArray.indexOf(e.currentTarget.dataset.url) > -1){
      //10007分队管理，10008代表处管理，10009分队管理+代表处管理
      if(_this.data.userInfo.grade_id != "10007" && _this.data.userInfo.grade_id != "10008"  && _this.data.userInfo.grade_id != "10009"){
        App.showError("您没有权限");
        return 
      }
    }
    if(e.currentTarget.dataset.url=="pages/team/photo/index" && _this.data.userInfo.grade_id == "10008"){
      App.showError("您不是队长权限不够用");
      return
    }
    // 记录formId
    App.saveFormId(e.detail.formId);
    wx.navigateTo({
      url: '/' + e.currentTarget.dataset.url
    })
  },

  /**
   * 跳转我的钱包页面
   */
  onTargetWallet(e) {
    let _this = this;
    if (!_this.onCheckLogin()) {
      return false;
    }
    // 记录formId
    App.saveFormId(e.detail.formId);
    wx.navigateTo({
      url: './wallet/index'
    })
  },

  /**
   * 跳转积分明细页
   */
  onTargetPoints(e) {
    let _this = this;
    if (!_this.onCheckLogin()) {
      return false;
    }
    // 记录formId
    App.saveFormId(e.detail.formId);
    wx.navigateTo({
      url: '../points/log/index'
    });
  },

  /**
   * 跳转到登录页
   */
  onLogin() {
    // wx.navigateTo({
    //   url: '../login/login',
    // });
    App.doLogin();
  },

  /**
   * 验证是否已登录
   */
  onCheckLogin() {
    let _this = this;
    if (!_this.data.isLogin) {
      App.showError('很抱歉，您还没有登录');
      return false;
    }
    return true;
  },
  onChooseAvatar(e) {
    const { avatarUrl } = e.detail;
    console.log(e.detail.avatarUrl);
    let av=e.detail.avatarUrl;
    let av1=wx.getFileSystemManager().readFileSync(av,'base64');
    let avatarUrl1 = "data:image/jpeg;base64,"+av1;
    console.log(avatarUrl1)
    this.setData({
      avatarUrl,
    })
    App._get('user.index/upUserInfo', {avatarUrl:avatarUrl1}, function(result) {
      console.log(result.data);
    });
    console.log(avatarUrl);
  },
  _bindblur(e){
    console.log(e.detail.value);    //获取input输入框的值
    this.setData({
      nickName:e.detail.value,
    })
    App._get('user.index/upUserInfo', {nickName:e.detail.value}, function(result) {
      console.log(result.data);
    });
  },
//----------------------------------------------
  //页面跳转
  pageTo(e){
    let url = e.currentTarget.dataset.value;
     console.log(url);
    wx.navigateTo({
      url:url
    })
  },

 
})